package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.GoodsModel;
import com.tiantianyuegou.view.MyDialogTextView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/1/27.
 */
public class CollectionGoodsAdapter extends BaseAdapter {
    private Context context;
    private List<GoodsModel> data;
    private PopupWindow popupWindow;
    private MyDialogTextView mDialog;

    public CollectionGoodsAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public CollectionGoodsAdapter(Context context, List<GoodsModel> data, Callback callback) {
        this.data = new ArrayList<>();
        this.context = context;
        this.data = data;
        mCallback = callback;
    }

    public void setData(List<GoodsModel> data) {
        this.data.clear();
        this.data.addAll(data);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View converView, final ViewGroup parent) {
        final GoodsModel model = data.get(position);
        final ViewHolder holder;
        if (converView == null) {
            converView = LayoutInflater.from(context).inflate(R.layout.grid_item_shoucang, null);
            holder = new ViewHolder();
            holder.name = (TextView) converView.findViewById(R.id.tv_goodsname);
            holder.price = (TextView) converView.findViewById(R.id.tv_goodsprice);
            holder.heart = (ImageView) converView.findViewById(R.id.iv_heart);
            holder.photo = (ImageView) converView.findViewById(R.id.iv_goods);
            converView.setTag(holder);
        } else {
            holder = (ViewHolder) converView.getTag();
        }
        holder.name.setText(model.getGoodsName()+"\n");
        Double price= Double.valueOf(model.getGoodsPrice());
        holder.price.setText("¥" + new DecimalFormat("0.00").format(price));
//        ImageLoader.getInstance().init(
//                ImageLoaderConfiguration.createDefault(context));
        ImageLoader.getInstance().displayImage(model.getGoodsPhoto(), holder.photo);

        final View finalConverView = converView;
        holder.heart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String content = "您确定取消收藏吗？";
                String title = "温馨提示:";
                mDialog = new MyDialogTextView(context,title,content);
                mDialog.setDialogCallback(new MyDialogTextView.DialogCallback() {
                    @Override
                    public void dialogOpt() {
                        mCallback.cancelCollect(position);
                    }
                });
                mDialog.show();

//                popupWindow = new PopupWindow();
//                View views = LayoutInflater.from(context).inflate(R.layout.popupwindow_deleaddress,
//                        null);
//                TextView txt_cancel2 = (TextView) views.findViewById(R.id.txt_cancel2);
//                txt_cancel2.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        popupWindow.dismiss();
//                    }
//                });
//                TextView txt_confirm = (TextView) views.findViewById(R.id.txt_confirm);
//                txt_confirm.setOnClickListener(new View.OnClickListener() {
//
//                    @Override
//                    public void onClick(View v) {
//                        mCallback.cancelCollect(position);
//                        popupWindow.dismiss();
//
//                    }
//                });
//                popupWindow = new PopupWindow(views, ViewPager.LayoutParams.MATCH_PARENT,
//                        ViewPager.LayoutParams.MATCH_PARENT, true);
//                popupWindow.setFocusable(true);
//                popupWindow.setOutsideTouchable(true);
//                popupWindow.setBackgroundDrawable(new BitmapDrawable());
//                popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);

            }
        });
        return converView;
    }

    private Callback mCallback;

    public interface Callback {
        void cancelCollect(int position);
    }

    class ViewHolder {
        TextView name;
        TextView price;
        ImageView heart;
        ImageView photo;
    }
}


